package prisd.brand.com.threegangswitch.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.telephony.TelephonyManager;

import java.util.HashMap;

public class NetworkUtil {
    public static final int CHINA_MOBILE = 1;
    public static final int CHINA_UNICOM = 2;
    public static final int CHINA_TELECOM = 3;
    public static final int SIM_OK = 0;
    public static final int SIM_NO = -1;
    public static final int SIM_UNKNOW = -2;
    public static boolean proxy = false;
    private static BroadcastReceiver connChangerRvr;
    public static final String CONN_TYPE_WIFI = "wifi";
    public static final String CONN_TYPE_GPRS = "gprs";
    public static final String CONN_TYPE_NONE = "none";

    public NetworkUtil() {
    }

    public static boolean isNetworkAvailable(Context context) {
        boolean result = false;
        if (getNetworkType(context) != null) {
            result = true;
        }

        return result;
    }

    private static String getNetworkType(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager)((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE));
        String result;
        if (connectivity == null) {
            result = null;
        } else {
            try {
                NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
                if (networkInfo == null) {
                    result = null;
                } else {
                    String temp = networkInfo.getExtraInfo();
                    result = networkInfo.getTypeName() + " " + networkInfo.getSubtypeName() + temp;
                }
            } catch (Exception var5) {
                var5.printStackTrace();
                result = null;
            }
        }

        return result;
    }

    public static int getSimState(Context context) {
        TelephonyManager telMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        int simState = telMgr.getSimState();
        if (simState == 5) {
            return 0;
        } else {
            return simState == 1 ? -1 : -2;
        }
    }

    public static int getNSP(Context context) {
        if (getSimState(context) != 0) {
            return -1;
        } else {
            TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            String operator = tm.getNetworkOperatorName().replaceAll(" ", "");
            String numeric = tm.getNetworkOperator();
            if ((operator == null || "".equals("")) && numeric != null) {
                operator = numeric;
            }

            if (operator != null && operator.length() != 0) {
                if (operator.compareToIgnoreCase("中国移动") != 0 && operator.compareToIgnoreCase("CMCC") != 0 && operator.compareToIgnoreCase("ChinaMobile") != 0 && operator.compareToIgnoreCase("46000") != 0) {
                    if (operator.compareToIgnoreCase("中国电信") != 0 && operator.compareToIgnoreCase("ChinaTelecom") != 0 && operator.compareToIgnoreCase("46003") != 0 && operator.compareToIgnoreCase("ChinaTelcom") != 0 && operator.compareToIgnoreCase("460003") != 0) {
                        if (operator.compareToIgnoreCase("中国联通") != 0 && operator.compareToIgnoreCase("ChinaUnicom") != 0 && operator.compareToIgnoreCase("46001") != 0 && operator.compareToIgnoreCase("CU-GSM") != 0 && operator.compareToIgnoreCase("CHN-CUGSM") != 0 && operator.compareToIgnoreCase("CHNUnicom") != 0) {
                            String imsi = PhoneUtil.getImsi(context);
                            if (!imsi.startsWith("46000") && !imsi.startsWith("46002") && !imsi.startsWith("46007")) {
                                if (imsi.startsWith("46001")) {
                                    return 2;
                                } else {
                                    return imsi.startsWith("46003") ? 3 : -2;
                                }
                            } else {
                                return 1;
                            }
                        } else {
                            return 2;
                        }
                    } else {
                        return 3;
                    }
                } else {
                    return 1;
                }
            } else {
                return -2;
            }
        }
    }

    public static void supportWap(Context context) {
        supportWap(context, new NetworkUtil.DefaultApnUriGetter());
    }

    public static void supportWap(Context context, NetworkUtil.ApnUriGetter uriGetter) {
        HashMap<String, String> param = null;
        Uri[] uris = uriGetter.getUriList();

        for(int i = 0; i < uris.length; ++i) {
            if (uris[i] != null) {
                param = getProxyInfo(context, uris[i]);
            }

            if (param != null) {
                setProxy((String)param.get("host"), (String)param.get("port"));
                break;
            }
        }

        IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
        connChangerRvr = new NetworkUtil.ConnectionChangeReceiver(uriGetter);
        context.registerReceiver(connChangerRvr, filter);
    }

    public static void unRegNetWorkRev(Context context) {
        setProxy((String)null, (String)null);

        try {
            if (connChangerRvr != null) {
                context.unregisterReceiver(connChangerRvr);
            }

        } catch (Exception var2) {
            var2.printStackTrace();
        }
    }

    public static void setProxy(String host, String port) {
        if (host != null && host.length() != 0) {
            proxy = true;
            System.getProperties().put("proxySet", "true");
            System.getProperties().put("proxyHost", host);
            if (port != null && port.length() > 0) {
                System.getProperties().put("proxyPort", port);
            } else {
                System.getProperties().put("proxyPort", "80");
            }
        } else {
            System.getProperties().put("proxySet", "false");
            proxy = false;
        }

    }

    public static HashMap<String, String> getProxyInfo(Context context, Uri uri) {
        String result = getNetworkType(context);
        HashMap<String, String> proxy = new HashMap();
        if (result == null) {
            return null;
        } else if (!result.contains("WIFI") && result.compareToIgnoreCase("MOBILE UMTS") != 0) {
            Cursor cr = null;

            try {
                cr = context.getContentResolver().query(uri, (String[])null, "mcc ='460'", (String[])null, (String)null);
                if (cr == null || !cr.moveToFirst()) {
                    return null;
                } else {
                    do {
                        if (cr.getCount() > 0) {
                            proxy.put("host", cr.getString(cr.getColumnIndex("proxy")));
                            proxy.put("port", cr.getString(cr.getColumnIndex("port")));
                            String apn = cr.getString(cr.getColumnIndex("apn"));
                            if (result.contains(apn)) {
                                HashMap var6 = proxy;
                                return var6;
                            }
                        }
                    } while(cr.moveToNext());

                    return null;
                }
            } catch (Exception var10) {
                var10.printStackTrace();
                return null;
            } finally {
                if (cr != null) {
                    cr.close();
                }

            }
        } else {
            return proxy;
        }
    }

    public static String getNetConnType(Context context) {
        ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (null == connManager) {
            return "none";
        } else {
            NetworkInfo info = null;
            info = connManager.getNetworkInfo(1);
            NetworkInfo.State mobileState;
            if (null != info) {
                mobileState = info.getState();
                if (NetworkInfo.State.CONNECTED == mobileState) {
                    return "wifi";
                }
            }

            info = connManager.getNetworkInfo(0);
            if (null != info) {
                mobileState = info.getState();
                if (NetworkInfo.State.CONNECTED == mobileState) {
                    return "gprs";
                }
            }

            return "none";
        }
    }

    public static boolean networkUsable(Context ctx) {
        String connType = getNetConnType(ctx);
        return !connType.equals("none");
    }

    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(1);
            if (mWiFiNetworkInfo != null) {
                return mWiFiNetworkInfo.isAvailable();
            }
        }

        return false;
    }

    public static class ConnectionChangeReceiver extends BroadcastReceiver {
        private NetworkUtil.ApnUriGetter uriGetter;

        public ConnectionChangeReceiver(NetworkUtil.ApnUriGetter uriGetter) {
            this.uriGetter = uriGetter;
        }

        public void onReceive(Context context, Intent intent) {
            HashMap<String, String> param = null;
            Uri[] uris = this.uriGetter.getUriList();

            for(int i = 0; i < uris.length; ++i) {
                if (uris[i] != null) {
                    param = NetworkUtil.getProxyInfo(context, uris[i]);
                }

                if (param != null) {
                    NetworkUtil.setProxy((String)param.get("host"), (String)param.get("port"));
                    break;
                }
            }

        }
    }

    public static class DefaultApnUriGetter implements NetworkUtil.ApnUriGetter {
        public DefaultApnUriGetter() {
        }

        public Uri[] getUriList() {
            Uri[] uris = new Uri[]{Uri.parse("content://telephony/carriers/preferapn"), Uri.parse("content://telephony/carriers/current")};
            return uris;
        }
    }

    public interface ApnUriGetter {
        Uri[] getUriList();
    }
}
